package com.reggie.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.reggie.acl.dataobject.Dish;
import com.reggie.acl.dataobject.SetmealDish;
import com.reggie.acl.mapper.SetmealDishMapper;
import com.reggie.service.service.SetmealDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 套餐菜品关系 服务实现类
 * </p>
 *
 * @author yth
 * @since 2023-02-04
 */
@Service
public class SetmealDishServiceImpl implements SetmealDishService {

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public List<SetmealDish> getBySetmealId(long setmealId) {
        LambdaQueryWrapper<SetmealDish> wrapper = Wrappers.lambdaQuery(SetmealDish.class);
        wrapper.eq(SetmealDish::getSetmealId, setmealId);
        return setmealDishMapper.selectList(wrapper);
    }

    @Override
    public List<SetmealDish> getBySetmealIds(Collection<Long> setmealIds) {
        LambdaQueryWrapper<SetmealDish> wrapper = Wrappers.lambdaQuery(SetmealDish.class);
        wrapper.in(SetmealDish::getSetmealId, setmealIds);
        return setmealDishMapper.selectList(wrapper);
    }

    @Override
    public void removeBatchSetmealIds(List<Long> ids) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(wrapper);
    }

    @Override
    public void save(List<SetmealDish> setmealDishes) {
        // TODO 待优化
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDishMapper.insert(setmealDish);
        }
    }

    @Override
    public void updateDish(Dish dish) {
        LambdaUpdateWrapper<SetmealDish> wrapper = Wrappers.lambdaUpdate(SetmealDish.class);
        wrapper.eq(SetmealDish::getDishId, dish.getId())
                .set(SetmealDish::getName, dish.getName())
                .set(SetmealDish::getPrice, dish.getPrice());
        setmealDishMapper.update(new SetmealDish(), wrapper);
    }

    @Override
    public int countDishByIds(List<Long> dishIds) {
        LambdaQueryWrapper<SetmealDish> wrapper = Wrappers.lambdaQuery(SetmealDish.class)
                .in(SetmealDish::getDishId, dishIds);
        return setmealDishMapper.selectCount(wrapper);
    }
}
